import { locale } from "@/mdesign-lib/mdesign-app.umd.js";
import { Action } from "@/mdesign-lib/mdesign-app.umd.js";
import { archService } from "@/mdesign-lib/mdesign-app.umd.js";
import { ActionKey } from "@/mdesign-lib/mdesign-app.umd.js";
import { projectService } from "@/mdesign-lib/mdesign-app.umd.js";
import { confirmWindow } from "@/mdesign-lib/mdesign-app.umd.js";
import { actionManager } from "@/mdesign-lib/mdesign-app.umd.js";
import { loading } from "@/mdesign-lib/mdesign-app.umd.js";

export class EWXSyncAction extends Action {
  keyName = "EWXSync" as ActionKey;
  desc = "EWXSync";

  async run() {
    const projectId = app.activeProject?.id;
    if (!projectId) throw new Error("无激活项目！");
    projectService
      .queryEWXConnectionState("127.0.0.1", "50001", false)
      .then(async () => {
        loading.popup(locale.t("模型库数据同步中") + "...");
        await archService.vireSync(projectId).finally(() => loading.close());
        app.$notify({ title: "成功", type: "success", message: "同步完成" });
      })
      .catch(async () => {
        const res = await confirmWindow.popup({
          title: "提示",
          content: "当前软件与EWX未完成集成连接，请先完成软件连接后再试。",
          okText: "去连接",
          status: "warn",
          cancelText: "取 消"
        });
        if (res.success) {
          const EWXAction = actionManager.getAction("EWX");
          EWXAction?.run();
        }
      });
  }

  onSysMenuClick() {
    this.run();
  }
}
